package cn.minimelon.cushaw.view.platform;

import cn.minimelon.bittermelon.bean.AjaxResult;
import cn.minimelon.bittermelon.bean.LoginUser;
import cn.minimelon.bittermelon.exception.WebException;
import cn.minimelon.bittermelon.solon.BaseController;
import cn.minimelon.bittermelon.utils.Melon;
import cn.minimelon.cushaw.entity.SysArea;
import cn.minimelon.cushaw.entity.SysMenu;
import cn.minimelon.cushaw.service.SysAreaService;
import cn.minimelon.cushaw.service.SysMenuService;
import org.noear.solon.core.handle.Context;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import java.util.List;

/**
 * 登录成功后，首页获取信息
 *
 * @author hiro
 */
@Controller
@Mapping("/a/portal")
public class PortalController extends BaseController {
    @Inject
    SysAreaService areaService;

    @Inject
    SysMenuService menuService;

    /**
     * 获取主题域信息，即：子系统
     *
     * @return 主题域
     */
    @Get
    @Mapping("/areas")
    public AjaxResult<Object> getAreas() {
        LoginUser user = this.getLoginUser();
        System.err.println(user);
        List<SysArea> areaList = areaService.selectAll(user);
        return Melon.success("success", areaList);
    }

    @Get
    @Mapping("/menus/{area}")
    public AjaxResult<Object> getMenus(String area) {
        LoginUser user = this.getLoginUser();
        List<SysMenu> menuList;
        if ("platform".equals(area)) {
            menuList = menuService.selectAll(user);
        } else {
            menuList = menuService.selectAreaMenus(area);
        }
        return Melon.success("success", menuList);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @Get
    @Mapping("getInfo")
    public AjaxResult<Object> getInfo() {
        return Melon.success();
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @Get
    @Mapping("getRouters")
    public AjaxResult<Object> getRouters() {
        return Melon.success();
    }

    @Get
    @Mapping("getError")
    public AjaxResult<Object> getError() {
        throw new WebException("100001", "test error");
    }
}
